import getUUID from '../utils/getUUID.js'
import { lazyReportCache } from '../utils/report.js'

/**
 * vue-router 监听
 * */
export default function onVueRouter(router) {
    router.beforeEach((to, from, next) => {
        // 首次加载页面不用统计
        if (!from.name) {
            return next()
        }
        const routerData = {
            params: to.params,
            query: to.query,
        }

        const data = {
            type: 'behavior',
            subType: ['vue-router-change', 'pv'],
            startTime: performance.now(),
            name: to.name || to.path,
            from: from.fullPath,
            to: to.fullPath,
            data: routerData,
            uuid: getUUID(),
        }
        lazyReportCache(data)
        next()
    })
}
